package com.wlscale.roadsystem.wiget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wlscale.roadsystem.R;

public class CarTypePopPopupWindow extends PopupWindow {
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 弹出框布局
     */
    private View mContentView;
    /**
     * 下拉列表
     */
    private RecyclerView listView;
    /**
     * 适配器
     */
    private CarTypeAdpter adpter;
    /**
     * 点击事件
     */
    private ReslutOnClickLister reslutOnClickLister;

    public CarTypePopPopupWindow(Context context, ReslutOnClickLister reslutOnClickLister) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContentView = inflater.inflate(R.layout.pop_cartype_list, null);
        this.reslutOnClickLister = reslutOnClickLister;
        initView();
    }

    private void initView() {
        //设置PopupWindow的View
        this.setContentView(mContentView);
        //设置弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置弹出窗体可点击
        this.setFocusable(true);
        //设置弹出窗体动画效果
//        this.setAnimationStyle(R.style.myTranslate);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
        setBackgroundAlpha((Activity) mContext, 1.0f);
        initListView();
    }

    private void initListView() {
        listView = mContentView.findViewById(R.id.listview);
        adpter = new CarTypeAdpter();
        listView.setLayoutManager(new LinearLayoutManager(mContext));
        listView.setAdapter(adpter);
    }

    private class CarTypeAdpter extends RecyclerView.Adapter<CarTypeAdpter.CarTypeItemHolder> {

        @Override
        public int getItemCount() {
            return 23;
        }

        @NonNull
        @Override
        public CarTypeItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            return new CarTypeAdpter.CarTypeItemHolder(LayoutInflater.from(mContext).inflate(R.layout.item_cartype_list, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull CarTypeItemHolder carTypeItemHolder, int i) {
            int carType = getCarTypeImage(i);//车型
            int axisType = getAxisTypeImage(i);//轴型
            carTypeItemHolder.iv_cartype_image.setImageResource(carType);
            carTypeItemHolder.iv_axis_type_image.setImageResource(axisType);
            carTypeItemHolder.max_weight_text_view.setText(i+"");

        }

        /**
         * 获取车型图片
         *
         * @param i
         */
        private int getCarTypeImage(int i) {
            int carType = 0;
            if (i == 0) {
                carType = R.mipmap.cartype_2;
            } else if (i == 1) {
                carType = R.mipmap.cartype_3_1;
            } else if (i == 2) {
                carType = R.mipmap.cartype_3_2;
            } else if (i == 3) {
                carType = R.mipmap.cartype_3_3;
            } else if (i == 4) {
                carType = R.mipmap.cartype_3_4;
            } else if (i == 5) {
                carType = R.mipmap.cartype_4_1;
            } else if (i == 6) {
                carType = R.mipmap.cartype_4_2;
            } else if (i == 7) {
                carType = R.mipmap.cartype_4_3;
            } else if (i == 8) {
                carType = R.mipmap.cartype_4_4;
            } else if (i == 9) {
                carType = R.mipmap.cartype_4_5;
            } else if (i == 10) {
                carType = R.mipmap.cartype_5;
            } else if (i == 11) {
                carType = R.mipmap.cartype_5_1;
            } else if (i == 12) {
                carType = R.mipmap.cartype_5_1_1;
            } else if (i == 13) {
                carType = R.mipmap.cartype_5_1_2;
            } else if (i == 14) {
                carType = R.mipmap.cartype_5_1_3;
            } else if (i == 15) {
                carType = R.mipmap.cartype_5_1_4;
            } else if (i == 16) {
                carType = R.mipmap.cartype_5_2;
            } else if (i == 17) {
                carType = R.mipmap.cartype_5_3;
            } else if (i == 18) {
                carType = R.mipmap.cartype_6_1;
            } else if (i == 19) {
                carType = R.mipmap.cartype_6_2;
            } else if (i == 20) {
                carType = R.mipmap.cartype_6_3;
            } else if (i == 21) {
                carType = R.mipmap.cartype_6_4;
            } else if (i == 22) {
                carType = R.mipmap.cartype_n;
            }
            return carType;
        }

        /**
         * 获取轴型图片
         *
         * @param i
         */
        private int getAxisTypeImage(int i) {
            int axisType = 0;
            if (i == 0) {
                axisType = R.mipmap.axistype_2;
            } else if (i == 1) {
                axisType = R.mipmap.axistype_3_1;
            } else if (i == 2) {
                axisType = R.mipmap.axistype_3_2;
            } else if (i == 3) {
                axisType = R.mipmap.axistype_3_3;
            } else if (i == 4) {
                axisType = R.mipmap.axistype_3_4;
            } else if (i == 5) {
                axisType = R.mipmap.axistype_4_1;
            } else if (i == 6) {
                axisType = R.mipmap.axistype_4_2;
            } else if (i == 7) {
                axisType = R.mipmap.axistype_4_3;
            } else if (i == 8) {
                axisType = R.mipmap.axistype_4_4;
            } else if (i == 9) {
                axisType = R.mipmap.axistype_4_5;
            } else if (i == 10) {
                axisType = R.mipmap.axistype_5_1;
            } else if (i == 11) {
                axisType = R.mipmap.axistype_5_1_1;
            } else if (i == 12) {
                axisType = R.mipmap.axistype_5_1_2;
            } else if (i == 13) {
                axisType = R.mipmap.axistype_5_1_3;
            } else if (i == 14) {
                axisType = R.mipmap.axistype_5_1_4;
            } else if (i == 15) {
                axisType = R.mipmap.axistype_5_2;
            } else if (i == 16) {
                axisType = R.mipmap.axistype_5_3;
            } else if (i == 17) {
                axisType = R.mipmap.axistype_6_1;
            } else if (i == 18) {
                axisType = R.mipmap.axistype_6_2;
            } else if (i == 19) {
                axisType = R.mipmap.axistype_6_3;
            } else if (i == 20) {
                axisType = R.mipmap.axistype_6_4;
            } else if (i == 21) {
                axisType = R.mipmap.axistype_6_5;
            } else if (i == 22) {
                axisType = R.mipmap.axistype_n;
            }
            return axisType;
        }


        class CarTypeItemHolder extends RecyclerView.ViewHolder {
            /**
             * 车型图片
             */
            private ImageView iv_cartype_image;
            /**
             * 车轴图片
             */
            private ImageView iv_axis_type_image;

            private TextView max_weight_text_view;

            public CarTypeItemHolder(@NonNull View itemView) {
                super(itemView);
                iv_cartype_image = itemView.findViewById(R.id.iv_cartype_image);
                iv_axis_type_image = itemView.findViewById(R.id.iv_axis_type_image);
                max_weight_text_view =  itemView.findViewById(R.id.max_weight_text_view);
            }
        }


    }


    public void show(View view) {
        showAsDropDown(view);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }

        super.showAsDropDown(anchor);

    }

    @Override
    public void dismiss() {
        setBackgroundAlpha((Activity) mContext, 1.0f);
        super.dismiss();
    }

    /**
     * 设置页面的透明度
     *
     * @param bgAlpha 1表示不透明
     */
    private static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            //不移除该Fl ag的话,在有视频的页面上的视频会出现黑屏的bug
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            //此行代码主要是解决在华为手机上半透明效果无效的bug
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        activity.getWindow().setAttributes(lp);
    }

    public interface ReslutOnClickLister {
    }
}
